package com.stu.controller;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
import com.stu.utils.XmlUtils;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.List;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: MI
 * @Date: 2021/11/14/16:29
 * @Description:
 */
@RestController
@RequestMapping("/xml")
public class XmlController {
	//application/xml 指定返回xml格式的数据，此处不指定默认使用json
	// @PostMapping(value = "/", produces = "application/xml;charset=UTF-8",)

	/**
	 * <User>
	 * <name>xmlUser</name>
	 * <id>133</id>
	 * <sex>男</sex>
	 * <age>18</age>
	 * </User>
	 */
	@PostMapping(value = "/" /* produces = MediaType.APPLICATION_XML_VALUE*/)
	public User xml(@Validated @RequestBody User users, BindingResult result) {
		users.setNow(LocalDateTime.now());
		// 如果 BindingResult 的 hasErrors 方法返回true，则表示有错误信息
		if (result.hasErrors()) {
			List<ObjectError> allErrors = result.getAllErrors();
			/// 遍历错误信息，返回给前端
			for (ObjectError error : allErrors) {
				System.out.println(error.getObjectName() + error.getDefaultMessage());
			}
		}
		System.out.println(users);
		return users;
	}

	@PostMapping(value = "/xmlstr", consumes = MediaType.APPLICATION_XML_VALUE, produces = MediaType.APPLICATION_XML_VALUE)
	public String xmlStr(@RequestBody String userStr) {
		String s = userStr;
		System.out.println(userStr);
		try {
			User users = XmlUtils.jacksonXML2Bean(userStr, User.class);
			users.setNow(LocalDateTime.now());
			// 如果 BindingResult 的 hasErrors 方法返回true，则表示有错误信息
			s = XmlUtils.jacksonBean2XML(users);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		return s;
	}

	@GetMapping(value = "/date")
	@ResponseBody
	public User date() {
		User user = new User();
		user.setNow(LocalDateTime.now());
		return user;
	}

	@PostMapping("jsonxml")
	@ResponseBody
	public LocalDateTime jsonXml(@RequestBody String body) {
		JSONObject jsonObject = JSONObject.parseObject(body);
		System.out.println(jsonObject);

		return LocalDateTime.now();
	}

	public static void main(String[] args) {
		User user = new User();
		user.setName("xmlName");
		user.setId(1);
		System.out.println(XmlUtils.jacksonBean2XML(user));
	}

	//实体类，getter、setter已省略
	@JacksonXmlRootElement(localName = "USER")
	@Setter
	@Getter
	public static class User implements Serializable {

		@NotBlank
		private String name;
		private Integer id;
		private String sex;
		private Integer age;
		private LocalDateTime now;

		@Override
		public String toString() {
			return ToStringBuilder.reflectionToString(this, ToStringStyle.NO_CLASS_NAME_STYLE);
		}
	}


	@PostMapping("/post")
	public LocalDateTime postBean(String name, String age) {
		System.out.println(StringUtils.repeat("-*", 8));
		System.out.println(name + "  " + age);
		return LocalDateTime.now();
	}
}
